package cn.iocoder.yudao.module.famous.doctor.service.videohistory;

import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.mybatis.core.query.LambdaQueryWrapperX;
import cn.iocoder.yudao.framework.mybatis.core.util.MyBatisUtils;
import com.baomidou.mybatisplus.core.metadata.IPage;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;

import java.time.LocalDateTime;
import java.util.*;
import cn.iocoder.yudao.module.famous.doctor.controller.admin.videohistory.vo.*;
import cn.iocoder.yudao.module.famous.doctor.dal.dataobject.videohistory.VideoHistoryDO;
import cn.iocoder.yudao.framework.common.pojo.PageResult;

import cn.iocoder.yudao.module.famous.doctor.convert.videohistory.VideoHistoryConvert;
import cn.iocoder.yudao.module.famous.doctor.dal.mysql.videohistory.VideoHistoryMapper;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.framework.security.core.util.SecurityFrameworkUtils.getLoginUserId;
import static cn.iocoder.yudao.module.famous.doctor.enums.ErrorCodeConstants.*;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.collection.ListUtil;

/**
 * 我的视频记录 Service 实现类
 *
 * @author Test1
 */
@Service
@Validated
public class VideoHistoryServiceImpl implements VideoHistoryService {

    @Resource
    private VideoHistoryMapper videoHistoryMapper;

    @Override
    public Long createVideoHistory(VideoHistoryCreateReqVO createReqVO) {
        // 插入
        VideoHistoryDO videoHistory = VideoHistoryConvert.INSTANCE.convert(createReqVO);
        if (videoHistory.getVideoId()!=null) {
            VideoHistoryDO videoHistoryDO = videoHistoryMapper.selectOne(new LambdaQueryWrapperX<VideoHistoryDO>()
                    .eq(VideoHistoryDO::getVideoId, createReqVO.getVideoId())
                    .eq(VideoHistoryDO::getDeleted, 0)
                    .last("limit 1"));
            if (videoHistoryDO!=null) {
                videoHistoryDO.setCreateTime(LocalDateTime.now());
                videoHistoryMapper.updateById(videoHistoryDO);
            }
        }
        videoHistoryMapper.insert(videoHistory);
        // 返回
        return videoHistory.getId();
    }

    @Override
    public void updateVideoHistory(VideoHistoryUpdateReqVO updateReqVO) {
        // 校验存在
        validateVideoHistoryExists(updateReqVO.getId());
        // 更新
        VideoHistoryDO updateObj = VideoHistoryConvert.INSTANCE.convert(updateReqVO);
        videoHistoryMapper.updateById(updateObj);
    }

    @Override
    public void deleteVideoHistory(Long id) {
        // 校验存在
        validateVideoHistoryExists(id);
        // 删除
        videoHistoryMapper.deleteById(id);
    }

    private void validateVideoHistoryExists(Long id) {
        if (videoHistoryMapper.selectById(id) == null) {
            throw exception(VIDEO_HISTORY_NOT_EXISTS);
        }
    }

    @Override
    public VideoHistoryDO getVideoHistory(Long id) {
        return videoHistoryMapper.selectById(id);
    }

    @Override
    public List<VideoHistoryDO> getVideoHistoryList(Collection<Long> ids) {
        if (CollUtil.isEmpty(ids)) {
            return ListUtil.empty();
        }
        return videoHistoryMapper.selectBatchIds(ids);
    }

    @Override
    public PageResult<VideoHistoryDO> getVideoHistoryPage(VideoHistoryPageReqVO pageReqVO) {
        return videoHistoryMapper.selectPage(pageReqVO);
    }

    @Override
    public List<VideoHistoryDO> getVideoHistoryList(VideoHistoryExportReqVO exportReqVO) {
        return videoHistoryMapper.selectList(exportReqVO);
    }

    @Override
    public IPage<VideoMyHistoryRespVO> getMessage(PageParam param) {
        return videoHistoryMapper.getMessage(MyBatisUtils.buildPage(param),getLoginUserId());
    }

    @Override
    public void deletebatchVideoHistory(Collection<Long> ids) {
        videoHistoryMapper.deleteBatchIds(ids);
    }

}
